/**
 * 
 */
package GUI.main.renderers;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.ListCellRenderer;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;

import GUI.helpers.ProjectConstants;
import GUI.main.MainClient;

import officeserver.log_error.UserException;
import officeserver.report.Appointment;
import officeserver.users.AccessLevel;
import officeserver.users.PersonalInfo;

/**
 * @author mramsey3
 * 
 */
public class AppointmentListRenderer extends JPanel implements ListCellRenderer {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private JPanel logoPanel;
    private JPanel textPanel;
    private JPanel patientPanel;
    private JLabel patientLabel;
    private JLabel patientNameLabel;
    private JPanel doctorPanel;
    private JLabel doctorLabel;
    private JLabel doctorNameLabel;
    
    /**
     * 
     */
    public AppointmentListRenderer(){
	setLayout(new BorderLayout());
	add(getLogoPanel(),BorderLayout.WEST);
	add(getTextPanel(),BorderLayout.CENTER);
    }
    
    /**
     * @return
     */
    private JPanel getLogoPanel(){
	if(logoPanel == null){
	    logoPanel = new JPanel(){
		private static final long serialVersionUID = 1L;

		public void paintComponent(Graphics g) {
		    super.paintComponent(g);
		    g.setColor(new Color(153,0,0));
		    g.fillRect((getWidth()/2)-10, 0, 20, getHeight());
		    g.fillRect(0, (getHeight()/2)-10, getWidth(),20);
		}
	    };
	    logoPanel.setPreferredSize(new Dimension(50, 50));
	    logoPanel.setBorder(BorderFactory.createLineBorder(
		    ProjectConstants.BORDER_SUB_COLOR, 6));
	    logoPanel.setOpaque(false);
	}
	return logoPanel;
    }
    
    /**
     * @return
     */
    private JPanel getTextPanel(){
	if(textPanel == null){
	    textPanel = new JPanel();
	    textPanel.setLayout(new BorderLayout());
	    textPanel.setBorder(new MatteBorder(2,0,2,2,ProjectConstants.BORDER_SUB_COLOR));
	    textPanel.add(getPatientPanel(),BorderLayout.CENTER);
	    textPanel.add(getDoctorPanel(),BorderLayout.SOUTH);
	    textPanel.setOpaque(false);
	}
	return textPanel;
    }
    
    /**
     * @return
     */
    private JPanel getPatientPanel(){
	if(patientPanel == null){
	    patientPanel = new JPanel();
	    patientPanel.setLayout(new BorderLayout());
	    patientPanel.add(getPatientLabel(),BorderLayout.WEST);
	    patientPanel.add(getPatientNameLabel(),BorderLayout.CENTER);
	    patientPanel.setOpaque(false);
	}
	return patientPanel;
    }
    
    /**
     * @return
     */
    private JLabel getPatientLabel(){
	if(patientLabel == null){
	    patientLabel = new JLabel(  "Patient:  ");
	    patientLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
	    patientLabel.setForeground(ProjectConstants.BORDER_SUB_COLOR);
	    patientLabel.setOpaque(false);
	}
	return patientLabel;
    }
    
    /**
     * @return
     */
    private JLabel getPatientNameLabel(){
	if(patientNameLabel == null){
	    patientNameLabel = new JLabel();
	    patientNameLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 14));
	    patientNameLabel.setOpaque(false);
	}
	return patientNameLabel;
    }
    
    /**
     * @param toSet
     */
    private void setPatientNameLabel(String toSet){
	getPatientNameLabel().setText(toSet);
    }
    
    /**
     * @return
     */
    private JPanel getDoctorPanel(){
	if(doctorPanel == null){
	    doctorPanel = new JPanel();
	    doctorPanel.setLayout(new BorderLayout());
	    doctorPanel.add(getDoctorLabel(),BorderLayout.WEST);
	    doctorPanel.add(getDoctorNameLabel(),BorderLayout.CENTER);
	    doctorPanel.setOpaque(false);
	}
	return doctorPanel;
    }
    
    /**
     * @return
     */
    private JLabel getDoctorLabel(){
	if(doctorLabel == null){
	    doctorLabel = new JLabel(  "Doctor:  ");
	    doctorLabel.setFont(new Font(Font.SANS_SERIF, Font.BOLD, 14));
	    doctorLabel.setForeground(ProjectConstants.BORDER_SUB_COLOR);
	    doctorLabel.setOpaque(false);
	}
	return doctorLabel;
    }
    
    /**
     * @return
     */
    private JLabel getDoctorNameLabel(){
	if(doctorNameLabel == null){
	    doctorNameLabel = new JLabel();
	    doctorNameLabel.setFont(new Font(Font.SANS_SERIF, Font.PLAIN, 14));
	    doctorNameLabel.setOpaque(false);
	}
	return doctorNameLabel;
    }
    
    /**
     * @param toSet
     */
    private void setDoctorNameLabel(String toSet){
	getDoctorNameLabel().setText(toSet);
    }
    
    /**
     * @param pP
     * @param pD
     */
    private void updateText(PersonalInfo pP, PersonalInfo pD){
	setPatientNameLabel(pP.getFirstName()+" "+pP.getLastName());
	setDoctorNameLabel(pD.getFirstName()+" "+pD.getLastName());
    }
    
    public Component getListCellRendererComponent(JList list, Object value,
	    int arg2, boolean isSelected, boolean arg4) {
	Appointment appointment = (Appointment) value;
	try {
	    MainClient.getConnection().userSearch(appointment.getPatientID(),
		    AccessLevel.PATIENT);
	} catch (UserException e) {
	    e.printStackTrace();
	}

	PersonalInfo pInfoPatient = null;
	try {
	    pInfoPatient = MainClient.getConnection().getSearchPersonalInfo();
	} catch (UserException e1) {
	    e1.printStackTrace();
	}
	try {
	    MainClient.getConnection().userSearch(appointment.getDoctorID(),
		    AccessLevel.DOCTOR);
	} catch (UserException e) {
	    e.printStackTrace();
	}
	PersonalInfo pInfoDoctor = null;
	try {
	    pInfoDoctor = MainClient.getConnection().getSearchPersonalInfo();
	} catch (UserException e) {
	    e.printStackTrace();
	}
	updateText(pInfoPatient,pInfoDoctor);
	if (isSelected) {
	    setBackground(list.getSelectionBackground());
	    setForeground(list.getSelectionForeground());
	    setBorder(new LineBorder(ProjectConstants.BORDER_MAIN_COLOR, 2));
	} else {
	    setBackground(list.getBackground());
	    setForeground(list.getForeground());
	    setBorder(new LineBorder(Color.white, 2));
	}
	setEnabled(list.isEnabled());
	setFont(list.getFont());
	setOpaque(true);
	setPreferredSize(new Dimension(getWidth(), 52));

	return this;
    }
}
